﻿using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Linq;
using System;
using Kooboo.Commerce.AddIn;


namespace Kooboo.Commerce.Validators
{
  [Export(typeof(IValidator))]
    public class LessThan : IValidator
    {

        public string Name
        {
            get
            {
                return "LessThan";
            }
        }





        public bool Validate(string value, List<ValidatorParameter> args)
        {
            var maximum = args.Where(i => i.Name == "MaxValue").Select(i => i.Value).FirstOrDefault();


            if (string.IsNullOrWhiteSpace(maximum) == false)
            {
                decimal number;
                DateTime date;
                if (decimal.TryParse(maximum, out number))
                {
                    try
                    {
                        if (number < decimal.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else if (DateTime.TryParse(maximum, out date))
                {
                    try
                    {
                        if (date < DateTime.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {

                    if (maximum == value)
                    {
                        return false;
                    }
                    else
                    {

                        List<string> values = (new List<string>()
                        {
                            maximum,
                            value
                        })
                        .OrderBy(i => i)
                        .ToList();

                        if (values[0] == maximum)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        public List<ValidatorParameter> GetParameters()
        {
             return new List<ValidatorParameter>()
            {
                new ValidatorParameter{ Name = "MaxValue", Example = "(100)"}
            };
        }

      
    }
}